package com.zkingsoft.actions.admin;

import com.zkingsoft.services.CodeReviewService;
import com.zkingsoft.services.DataDictionaryService;
import com.zkingsoft.services.ProjUserService;
import com.zkingsoft.services.ReviewDetailService;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import com.zkingsoft.actions.common.BaseController;
import org.springframework.stereotype.Controller;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.WebUtil;

import org.springframework.web.bind.annotation.ResponseBody;
import com.zkingsoft.model.CodeReview;
import com.zkingsoft.model.DataDictionary;
import com.zkingsoft.model.ProjUser;
import com.zkingsoft.model.ReviewDetail;

import org.springframework.web.bind.annotation.RequestMapping;
import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.constance.Dictionary;

/**
 * This field was generated by Zking.software.Codegen.
 * @date 2016-11-10 16:57
 */
@Controller
@RequestMapping(value = "admin/codeReview")
public class CodeReviewController extends BaseController{

	@Resource
	private CodeReviewService codeReviewService;
	@Resource
	private ProjUserService projUserService;
	@Resource
	private DataDictionaryService dataDictionaryService;
	@Resource
	private ReviewDetailService reviewDetailService;
	
	/** 
	 * @Description:进入代码评审列表页
	 * @author:dingchuan
	 * @param projId
	 * @return
	 * 返回类型 String
	 * @date 2016年11月11日
	 */   
	@RequestMapping(value = "/entry")
	public String entry(Long projId){		
		ProjUser projUser = new ProjUser();
		projUser.setProjId(projId);
		List<ProjUser> projUserlist = projUserService.findByModel(projUser);
		WebUtil.getRequest().setAttribute("projUserlist", projUserlist);
		WebUtil.getRequest().setAttribute("projId", projId);
		return "admin/codeReview-list";
	}	
	
       	
	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(CodeReview codeReview, PaginationVO pageVo) {
		return showList(codeReviewService, codeReview, pageVo);
	}
   	
	/**
	 * 新增
	 */   
	@RemoveRequestToken	
   	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(CodeReview codeReview) {
		codeReview.setCreateUserId(getMe().getId());
		codeReview.setTime(new Date());
		List<ReviewDetail> reviewDetailList = codeReview.getReviewDetailList();
		int trueAnswer = 0;
		for(ReviewDetail reviewDetail : reviewDetailList){
			String answer = reviewDetail.getAnswer();
			if(Dictionary.CODE_REVIEW_YES.equals(answer)){
				trueAnswer += 1;
			}
		}
		double answerState = trueAnswer/(double)reviewDetailList.size()*100;
		DecimalFormat df = new DecimalFormat("#.##");    
		double score = Double.parseDouble(df.format(answerState));
		codeReview.setScore(score);
		return add(codeReviewService, codeReview, "代码评审");
	}
	
   	/**
	 * 进入修改界面
	 */   
	@SaveRequestToken
   	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		CodeReview codeReview;
		if (id != null) {
			codeReview = codeReviewService.findById(id);
			WebUtil.getRequest().setAttribute("obj", codeReview);
		}
		return "admin/codeReview-form";
	}
	

   	@RequestMapping(value = "/lookForm")
	public String lookForm(Long id) {
		CodeReview codeReview = codeReviewService.findById(id);
		ReviewDetail reviewDetail = new ReviewDetail();
		reviewDetail.setReviewId(id);
		List<ReviewDetail> list = reviewDetailService.findByModel(reviewDetail);
		WebUtil.getRequest().setAttribute("list", list);
		WebUtil.getRequest().setAttribute("obj", codeReview);
		return "admin/codeReview-look-form";
	}
	
	
	/**
	 * @Description:新增评审
	 * @author:dingchuan
	 * @param projId
	 * @return
	 * 返回类型 String
	 * @date 2016年11月11日
	 */
	@SaveRequestToken
	@RequestMapping(value = "/addForm")
	public String addForm(Long projId){
		ProjUser projUser = new ProjUser();
		projUser.setProjId(projId);
		List<ProjUser> projUserlist = projUserService.findByModel(projUser);
		DataDictionary dataDictionary = new DataDictionary();
		dataDictionary.setType(Dictionary.DICTIONARY_CODE_REVIEW);
		List<DataDictionary> ddtList = dataDictionaryService.findByModel(dataDictionary);		
		WebUtil.getRequest().setAttribute("projUserlist", projUserlist);
		WebUtil.getRequest().setAttribute("ddtList", ddtList);
		WebUtil.getRequest().setAttribute("projId", projId);
		return "admin/codeReview-add-form";
	}
	
   	
   	/**
	 * 删除
	 */  
 	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(codeReviewService, keys);
	}
  
}